const Koa = require('koa');

const bodyParser = require('koa-bodyparser');
const Jwt = require('./utils/jwt');

const controller = require('./controller');

const app = new Koa();

require('./plugins/db')()

// log request URL:
app.use(async (ctx, next) => {
  console.log(`Process ${ctx.request.method} ${ctx.request.url}...`);
  if (false) {
    //ctx.request.url !== '/login' && ctx.request.url !== '/register'
    let token = ctx.request.headers.token;
    console.log(token);
    const jwt = new Jwt(token);
    let result = jwt.verifyToken();
    // 校验通过 -- next;否则返回登陆信息不正确
    if (result == 'err') {
      ctx.body = { code: 403, msg: '登陆已过期，请重新登陆' }
    } else {
      await next();
    }
  } else {
    await next();
  }
});

//parse request body:
app.use(bodyParser());

//add controllers:
app.use(controller());

app.listen(3000);
console.log('app started at port 3000...');